package com.jhy.study2.IO;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.util.Date;

import static java.nio.file.StandardCopyOption.*;

/**
 * @author: robert
 * @description: IO的一些练习
 * @date: 2021/8/23 16:32
 * @return
 */
public class IOTestor1 {

    public static void main(String[] args)  {
//        basedFileTest();
//        pathTest();
        pathMoveTest();

        //还有部分属性的练习和过滤器的书写



    }

    public static void basedFileTest(){
        File file = new File("D://folder1//folder2"); //这么写会创建D:\bbb\aaa.txt(此时这是一个文件夹)
        if(!file.exists()){
            file.mkdirs();
        }
        File file3 = new File("D://folder1//folder2//folder3");
        if(!file3.exists()){
            file3.mkdirs();
        }

        System.out.println("file_isDirectory:" + file.isDirectory());

        File file2 = new File("D://folder1//folder2//abc.txt");
        if(!file2.exists()){  //现在这种情况是文件夹有了，文件没有，创建一个
            try {
                file2.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        System.out.println("执行完毕");
        System.out.println("file2_length:" + file2.length());
        System.out.println("file2_isDirectory:" + file2.isDirectory());
        System.out.println("file2_isFile:" + file2.isFile());
        System.out.println("file2_getName:" + file2.getName());
        System.out.println("file2_getPath:" + file2.getPath());
        System.out.println("file2_getParent:" + file2.getParent());
        System.out.println("file2_getAbsolutePath:" + file2.getAbsolutePath());
        System.out.println("file2_lastModified:" + new Date(file2.lastModified()));


        /*
        * file_isDirectory:true
执行完毕
file2_length:204
file2_isDirectory:false
file2_isFile:true
file2_getName:abc.txt
file2_getPath:D:\folder1\folder2\abc.txt
file2_getParent:D:\folder1\folder2
file2_getAbsolutePath:D:\folder1\folder2\abc.txt
file2_lastModified:Mon Aug 23 16:54:07 CST 2021
        *
        * */


        //展示一个目录
        System.out.println("==========展示一个目录：==============");
        File[] fs = file.listFiles();   //只会罗列当前目录
        System.out.println("file:"+ file.toString());
        System.out.println("file is one directory?:" + file.isDirectory());
        for(File f : fs) {
            System.out.println("name:"+ f.getName() + " ,path:"+ f.getPath());
        }

    }

    public static void pathTest(){
        File file = new File("D://folder1//folder2//folder3");
        //方式一：file转化为Path
        Path path = file.toPath();
        System.out.println("path_toString:"+path.toString());
        System.out.println("path_getFileName:"+path.getFileName());
        System.out.println("path_getNameCount:"+path.getNameCount());  //表示有几层文件(夹)
        System.out.println("path_getName(0):"+path.getName(0));

        System.out.println("==========================================");

        //方式二：直接申明
        Path path2 = FileSystems.getDefault().getPath("D://folder1//folder2","//abc.txt");
        System.out.println("path2:"+path2.toString());
        System.out.println("path2_getNameCount:"+path2.getNameCount());


        System.out.println("==========================================");

        //方式三： Paths.get(string1,strign2)
        Path path3 = Paths.get("D://folder1//folder2","//abc.txt");
        System.out.println("path3:"+path3.toString());
        System.out.println("path3_getNameCount:"+path3.getNameCount());
        System.out.println("path2.compareTo(path3) -->" + path2.compareTo(path3));

        System.out.println("合并path2和path3:"+ path2.resolve(path3));
        System.out.println("合并path2和path3:"+ path2.resolve("cccccccccc//dddd"));  //拼接多层，ccc前面不能有斜杠了
        Path path4 = Paths.get("c:/temp");
        System.out.println("path4: " + path4.resolve("abc.txt"));
    }

    public static void pathMoveTest(){
        Path from = Paths.get("D://folder1//folder2","abc.txt");
        Path to = from.getParent().resolve("folder3//abc.txt");
        System.out.println("to:"+to);
        try {
            System.out.println("Files.size(from):" + Files.size(from));
//            Files.move(from,to,REPLACE_EXISTING);
//            Files.move(from,to,COPY_ATTRIBUTES);  //Exception in thread "main" java.lang.UnsupportedOperationException: Unsupported copy option
            Files.move(from,to,ATOMIC_MOVE);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

}
